﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.Globalization;
using System.Globalization;

namespace Medianamik.UI.Web.ModalWindows
{
    public partial class DeleteResourceWindow : BasePage
    {
        string _cultureName;
        string _resourceId;
        string _resourceSet;
        const string DeleteResourceInstance = "DeleteResourceInstance";
        const string DeleteResource = "DeleteResource";
        const string DeleteResourceSet = "DeleteResourceSet";

        protected MedianamikResourceDataManager Manager = new MedianamikResourceDataManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            _cultureName = Request.QueryString["culture"];
            _resourceId = Request.QueryString["id"];
            _resourceSet = Request.QueryString["set"];

            if (!IsPostBack)
            {
                if (String.IsNullOrEmpty(_cultureName))
                {
                    DisplayDenyMessage(String.Concat(
                         (string)GetGlobalResourceObject("Medianamik_Global", "ArgumentNullException"), " ",
                         "culture"));
                }
                else if (_resourceId == null)  //Peut être empty
                {
                    DisplayDenyMessage(String.Concat(
                         (string)GetGlobalResourceObject("Medianamik_Global", "ArgumentNullException"), " ",
                         "id"));
                }
                else if (_resourceSet == null) //Peut être empty
                {
                    DisplayDenyMessage(String.Concat(
                         (string)GetGlobalResourceObject("Medianamik_Global", "ArgumentNullException"), " ",
                         "set"));
                }
                else
                {
                    CultureInfo culture = null;

                    try
                    {
                        culture = new CultureInfo(_cultureName);
                    }
                    catch
                    {
                        culture = null;
                    }

                    if (culture == null)
                    {
                        DisplayDenyMessage(String.Concat(
                         (string)GetGlobalResourceObject("Medianamik_Global", "InvalidArgumentException"), " ",
                         "culture"));
                    }
                    else
                    {
                        if (Manager.ResourceExists(_resourceId, _cultureName, _resourceSet))
                        {
                            deleteOptions.Items.Add(new ListItem(String.Concat(
                                (string)GetGlobalResourceObject("Medianamik_ResourceManagement", DeleteResourceInstance), " ",
                                _resourceId, " - ", culture.DisplayName), DeleteResourceInstance));
                        }

                        deleteOptions.Items.Add(new ListItem(String.Concat(
                            (string)GetGlobalResourceObject("Medianamik_ResourceManagement", DeleteResource), " ",
                            _resourceId), DeleteResource));

                        deleteOptions.Items.Add(new ListItem(String.Concat(
                            (string)GetGlobalResourceObject("Medianamik_ResourceManagement", DeleteResourceSet), " ",
                            _resourceSet), DeleteResourceSet));
                    }
                }
            }
        }

        protected void Delete(object sender, EventArgs e)
        {
            string reloadType = "";
            bool hasChanged = false;

            switch (deleteOptions.SelectedValue)
            {
                case DeleteResourceInstance:
                    Manager.DeleteResource(_resourceId, _cultureName, _resourceSet);
                    reloadType = "ReloadValue";
                    hasChanged = true;
                    break;
                case DeleteResource:
                    Manager.DeleteResource(_resourceId, "", _resourceSet);
                    hasChanged = true;
                    reloadType = "ReloadResourceIDs";
                    break;
                case DeleteResourceSet:
                    Manager.DeleteResourceSet(_resourceSet);
                    hasChanged = true;
                    reloadType = "ReloadResourceSets";
                    break;
            }

            if (hasChanged)
            {
                MedianamikResourceConfiguration.ClearResourceCache();

                if (_resourceSet.Equals("sitemap", StringComparison.OrdinalIgnoreCase))
                {
                    //Reset du Menu
                    ((MedianamikSiteMapProvider)SiteMap.Provider).Rebuild();
                }
            }

            ScriptManager.RegisterStartupScript(this, typeof(DeleteResourceWindow),
                "CloseWindow", "closeWindow('" + reloadType + "');", true);
        }

        void DisplayDenyMessage(string message)
        {
            scriptBlock.Visible = false;
            mainPlaceHolder.Visible = false;
            denyPlaceHolder.Visible = true;
            denyMessage.Text = message;
        }
    }
}
